<?php

/**
 * factory actions.
 *
 * @package    WacStorehouse
 * @subpackage factory
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class factoryActions extends WacCommonActions {
    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetCottonFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryCotton);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetDyeFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryDye);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetWeaveFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryWeave);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetCleanUpFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryCleanUp);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetWeaveCleanUpFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

        $types = array(WacFactoryType::$factoryWeave, WacFactoryType::$factoryCleanUp);
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByTypes($types);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetDyeWeaveFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();

        $types = array(WacFactoryType::$factoryDye, WacFactoryType::$factoryWeave);
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByTypes($types);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetFinalFactoryIdNameHashInFormat(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryFinal);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
    */
    public function executeGetStockGoodsCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"));

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as select html format
    */
    public function executeGetStockWarpYarnCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classDyeCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockFillingYarnCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classCottonCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
     */
    public function executeGetStockWeaveCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));
        if ($request->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classWeaveCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['type'] = $listObj->getWacFactoryType()->getName();
                $tmpArr['area_size_unit_code']    = $listObj->getWacAreaSizeUnit()->getName();
                $tmpArr['capacity_unit_code']     = $listObj->getWacCapacityUnit()->getName();
                $tmpArr['goods_amount_unit_code'] = $listObj->getWacGoodsAmountUnit()->getName();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }
}